﻿using UnityEngine;
using System.Collections;
using K;
public class BossAnimationCtr : MonoBehaviour 
{
    AnimatorCtr bossAnimator;
    public Animator animatorBoss;
    private bool bBossDeadth = false;

    public GameObject[] deadEffects;
    private GameObject deadEffect;
    private Rigidbody bossRigidbody;

	void Start () 
    {
        if(animatorBoss != null)
        {
            //bossAnimator = new AnimatorCtr(animatorBoss);
        }

        if (transform.Find("DeadEffect") != null)
	    {
            deadEffect = transform.Find("DeadEffect").gameObject;
	    }
        
	    if (deadEffect != null)
	    {
	        deadEffect.SetActive(false);
	    }
        for (int i = 0; i < deadEffects.Length; ++i)
        {
            deadEffects[i].SetActive(false);
        }
        bossRigidbody = transform.GetComponentInParent<Rigidbody>();
    }
	
	void Update () 
    {
        if (!bBossDeadth && deadEffect != null && deadEffect.activeInHierarchy)
	    {
            deadEffect.SetActive(false);
            if (deadEffects != null)
            {
                for (int i = 0; i < deadEffects.Length; ++i)
                {
                    deadEffects[i].SetActive(false);
                }
            }
	    }
        if (bossAnimator != null)
	    {
	        bossAnimator.Update();
            if (bossRigidbody != null && !bBossDeadth)
	        {
                animatorBoss.speed = bossRigidbody.velocity.magnitude / 200;
	        }
            else
            {
                animatorBoss.speed = 1;
            }
	    }
	}

    public void PlayBossAnimation(string animator, ELoopState eLoop, float fArg, float nTimes = 0, bool bForce = false)
    {
        if (bossAnimator != null)
        {
            bossAnimator.PlayPlayerAnimator(animator, eLoop, fArg, nTimes, bForce);
        }
    }

    public void BossAttackAnimation()
    {
        PlayBossAnimation("attack", ELoopState.emLoopOne, 1, 1);
    }

    public void BossShowAnimation()
    {
        PlayBossAnimation("kaichan2", ELoopState.emLoopOne, 1, 1);
    }

    public void BossBeAttackAnimation()
    {
        PlayBossAnimation("hit", ELoopState.emLoopTimes, 1, 1);
    }

    public void BossDeathAnimation()
    {
        PlayBossAnimation("death", ELoopState.emLoopTimes, 1, 100, true);
        //死亡特效
        if (deadEffect != null)
        {
            deadEffect.SetActive(true);
        }
        if (deadEffects != null)
        {
            for (int i = 0; i < deadEffects.Length; ++i)
            {
                deadEffects[i].SetActive(true);
            }
        }

        bBossDeadth = true;
    }
}